package com.bps.ksiazkaadresowa;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class OsobaDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private JButton okButton;
	private JButton cancelButton;
	private JTextField nazwiskoTextField;
	private JTextField imieTextField;
	private JTextField kodPocztowyTextField;
	private JTextField miastoTextField;
	private JTextField ulicaTextField;
	private JTextField telefonTextField;
	private JTextField telefon2TextField;
	public boolean result = false;
	
	public OsobaDialog(Component parent) {
		super();
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		setTitle("Edycja danych osobowych");
		setSize(400, 500);
		setLocationRelativeTo(parent);
		setModal(true);
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(5, 5, 5, 5);
		c.weightx = 0.1;
		c.weighty = 0.1;
		
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth++;
		add(new JLabel("Nazwisko:"), c);
		
		c.gridy++;
		nazwiskoTextField = new JTextField();
		add(nazwiskoTextField, c);
		
		c.gridy++;
		add(new JLabel("Imię:"), c);
		
		c.gridy++;
		imieTextField = new JTextField();
		add(imieTextField, c);
		
		c.gridy++;
		add(new JLabel("Kod pocztowy:"), c);
		
		c.gridy++;
		kodPocztowyTextField = new JTextField();
		add(kodPocztowyTextField, c);
		
		c.gridy++;
		add(new JLabel("Miasto:"), c);
		
		c.gridy++;
		miastoTextField = new JTextField();
		add(miastoTextField, c);
		
		c.gridy++;
		add(new JLabel("Ulica:"), c);
		
		c.gridy++;
		ulicaTextField = new JTextField();
		add(ulicaTextField, c);
		
		c.gridy++;
		add(new JLabel("Telefon:"), c);
		
		c.gridy++;
		telefonTextField = new JTextField();
		add(telefonTextField, c);
		
		c.gridy++;
		add(new JLabel("Telefon 2:"), c);
		
		c.gridy++;
		telefon2TextField = new JTextField();
		add(telefon2TextField, c);
		
		c.gridy++;
		c.gridwidth--;
		cancelButton = new JButton("Anuluj");
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				result = false;
				dispose();
			}
		});
		add(cancelButton, c);
		
		c.gridx++;
		okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				result = true;
				dispose();
			}
		});
		add(okButton, c);
		setVisible(false);
	}
	
	public Osoba getOsoba() {
		return new Osoba(nazwiskoTextField.getText(), imieTextField.getText(), kodPocztowyTextField.getText(), miastoTextField.getText(), ulicaTextField.getText(), telefonTextField.getText(), telefonTextField.getText());
	}
	
	public void setOsoba(Osoba osoba) {
		nazwiskoTextField.setText(osoba.getNazwisko());
		imieTextField.setText(osoba.getImie());
		kodPocztowyTextField.setText(osoba.getKodPocztowy());
		miastoTextField.setText(osoba.getMiasto());
		ulicaTextField.setText(osoba.getUlica());
		telefonTextField.setText(osoba.getTelefon());
		telefon2TextField.setText(osoba.getTelefon2());
	}
}
